#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <stdio.h>

#include "plplot.h"
#include "plplotP.h"


using namespace std;

static void rainbow_cmap()
{
	PLFLT pos[7] = {0.0, 0.17, 0.33, 0.50, 0.67, 0.83, 1.0};

	PLFLT r[7]   = {0.0,  0.0,  0.0,  0.6,  1.0,  1.0, 1.0};
	PLFLT g[7]   = {0.0,  0.0,  1.0,  1.0,  1.0,  0.6, 0.0};
	PLFLT b[7]   = {0.3,  0.8,  1.0,  0.3,  0.0,  0.0, 0.0};

	plscmap1l(1, 7, pos, r, g, b, NULL);
}

int main(int argc, char **argv)
{
    if (argc != 3) {
        fprintf(stderr, "Usage\n\t./image2.exe [nx] [ny]\n");
        exit(99);
    }

//    plsdev("xwin");
	plspage(0.,0.,800,800,0,0);
	plinit();

	int nx = atoi(argv[1]);
	int ny = atoi(argv[1]);

    FILE *dataFilePtr = fopen("imageData.txt","w");

    PLFLT **data;
    plAlloc2dGrid(&data,nx,ny);

	for (int y=0; y < ny; y++) {
		for (int x=0; x < nx; x++) {
			data[x][y] = x + nx * y;
			fprintf(dataFilePtr, "%d %d %4.2f\n",x,y,data[x][y]);
		}
    	fprintf(dataFilePtr, "\n");
	}

    double xmin = 0;
    double xmax = nx;
    double ymin = 0;
    double ymax = ny;
    double zmin = 0;
    double zmax = (nx * ny) - 1;

	/* Set the colour map */
	rainbow_cmap();

	pladv(0);
//	plvpor(0.15,0.75,0.2,0.8);
	plvpas(0.15,0.75,0.2,0.8,1.);
	plwind(xmin,xmax,ymin,ymax);
//    plenv(0., nx, 0, ny, 1, -1);
	char title[50];
	strcpy(title,"title");
	plcol0(15);
	plmtex("t", 4.0, 0.5, 0.5, title);
	plmtex("b", 4.0, 0.5, 0.5, "x");
	plmtex("l", 4.0, 0.5, 0.5, "y");

	plimage(data,
			(PLINT) nx,   (PLINT) ny,
			(PLFLT) xmin, (PLFLT) xmax,
			(PLFLT) ymin, (PLFLT) ymax,
			(PLFLT) zmin, (PLFLT) zmax,
			(PLFLT) xmin, (PLFLT) xmax,
			(PLFLT) ymin, (PLFLT) ymax);

	plcol0(15);
	plschr (0, 0.6);
	plbox("bcnst",0.0,0,"bcnst",0.0,0);
//	plbox("bcnst",xinc,4,"bcnst",yinc,4);
	plschr (0, 1.0);



plend();
exit(1);
    int nSteps = 256;

	/*======================================================================
	 * Scale
	 *======================================================================*/
	cout << endl;
	cout << "===================================================" << endl;
	cout << "Scale" << endl;
	cout << "===================================================" << endl;
	double **scale;
	int width = 2;
	scale = new double* [width];
	for (register int x=0;x<width;x++)
		scale[x] = new double [nSteps];

	double scaleInc = (zmax-zmin)/(nSteps-1);
	double scaleMin = zmin - (scaleInc/2.0);
	double scaleMax = zmax + (scaleInc/2.0);

	cout << "- zmin      = " << zmin     << endl;
	cout << "- zmax      = " << zmax     << endl;
	cout << "- scale min = " << scaleMin << endl;
	cout << "- scale max = " << scaleMax << endl;
	cout << "- scale inc = " << scaleInc << endl;

	for (int step=0;step<nSteps;step++) {
		for (int w=0;w<width;w++) {
			scale[w][step] = zmin + scaleInc * step;
		}
	}

	/* Plot scale */
	pladv(1);
	plvpor(0.79,0.82,0.2,0.8);
	plwind(0.0,xmax,zmin,zmax);
	plimage(scale, 				// 2d data array
			width,nSteps,		// data dimensions 	-
			xmin,xmax,			// x plot coordinates
//			scaleMin,scaleMax,	// y plot coordinates
			zmin,zmax,
			zmin,zmax,			// z range to which to plot data
			0.0,xmax,			// x dimensions of sub window to plot
			zmin,zmax);			// y dimensions of sub window to plot
//			scaleMin,scaleMax);	// y dimensions of sub window to plot
	plcol0(15);
	plschr (0, 0.6);
	plbox("bc",0.0,0,"cmst",((zmax-zmin)/5.0),5);
//	plbox("bc",0.0,0,"cmst",0.0,0);
//	plbox("bc",0.0,0,"cmst",scaleInc,1);
	plschr (0, 1.0);
	plcol0(15);
	plschr (0, 0.5);
	plmtex("l", 2.0, 1.0, 1, "amplitude scale");
	plschr (0, 1);

	plend();

	return EXIT_SUCCESS;
}
